package dao;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import model.News;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for News
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see model.News
 * @author MyEclipse Persistence Tools
 */

public class NewsDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(NewsDAO.class);
	// property constants
	public static final String NEWS_TITLE = "newsTitle";
	public static final String NEWS_REPORTER = "newsReporter";
	public static final String NEWS_CONTENT = "newsContent";

	protected void initDao() {
		// do nothing
	}

	public void save(News transientInstance) {
		log.debug("saving News instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(News transientInstance) {
		log.debug("updating News instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(News persistentInstance) {
		log.debug("deleting News instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public News findById(java.lang.Long id) {
		log.debug("getting News instance with id: " + id);
		try {
			News instance = (News) getHibernateTemplate().get("model.News", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(News instance) {
		log.debug("finding News instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding News instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from News as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findPropertysForPage(String[] propertys,final int offset, final int lengh,String orderProperty) {
		log.debug("findPropertysForPage News instance from " + offset
				+ ", value: " + lengh);
		try {
			StringBuffer sb = new StringBuffer();
			if(propertys.length>0){
				sb.append("select ");
				for(int i = 0 ; i < propertys.length-1 ; i++){
					sb.append("model.");
					sb.append(propertys[i]);
					sb.append(",");
				}
				sb.append("model.");
				sb.append(propertys[propertys.length-1]);
			}
			sb.append(" from News as model order by model."+orderProperty+" desc");
			final String hql = sb.toString();
			List list = getHibernateTemplate().executeFind(new HibernateCallback(){
				@Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    List list2 = session.createQuery(hql).setFirstResult(offset).setMaxResults(lengh).list();                    
                    return list2;
				}});
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByNewsTitle(Object newsTitle) {
		return findByProperty(NEWS_TITLE, newsTitle);
	}

	public List findByNewsReporter(Object newsReporter) {
		return findByProperty(NEWS_REPORTER, newsReporter);
	}

	public List findByNewsContent(Object newsContent) {
		return findByProperty(NEWS_CONTENT, newsContent);
	}

	public List findAll() {
		log.debug("finding all News instances");
		try {
			String queryString = "from News";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public News merge(News detachedInstance) {
		log.debug("merging News instance");
		try {
			News result = (News) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(News instance) {
		log.debug("attaching dirty News instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(News instance) {
		log.debug("attaching clean News instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static NewsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (NewsDAO) ctx.getBean("NewsDAO");
	}
}